package com.fowo.api.controller.purchase.impinterface;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.common.model.R;
import com.fowo.api.controller.purchase.vo.PurchaseOrderProductItem;
import com.fowo.api.datainterface.util.DingTalkUtil;
import com.fowo.api.entity.PurchaseOrder;
import com.fowo.api.entity.PurchaseOrderProduct;
import com.fowo.api.flow.config.DingDingConfig;
import com.fowo.api.flow.utils.DingDingUtil;
import com.fowo.api.mapper.WarehouseBinDMapper;
import com.fowo.api.model.purchase.order.PurchaseOrderMessagereminderAction;
import com.fowo.api.service.PurchaseOrderProductlogService;
import com.fowo.api.service.impl.PurchaseOrderProductServiceImpl;
import com.fowo.api.service.impl.PurchaseOrderProductlogServiceImpl;
import com.fowo.api.service.impl.PurchaseOrderServiceImpl;
import com.fowo.api.sys.entity.SysUser;
import com.fowo.api.sys.service.SysUserService;
import com.fowo.api.util.LocalDateUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Component()
public class purchaseOrderMessagereminderAction implements PurchaseOrderMessagereminderAction {

    @Resource
    private PurchaseOrderServiceImpl purchaseOrderService;
    @Resource
    protected PurchaseOrderProductServiceImpl purchaseOrderProductService;
    @Resource
    private WarehouseBinDMapper warehouseBinDMapper;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private DingDingConfig dingdingConfig;
    @Resource
    private DingDingUtil dingDingUtil;


    @Override
    public void run() throws Exception {
        Date  enddate = new Date();
        Date startDate = LocalDateUtils.addDateDays(enddate, -3);
        //获取待到货的订单
        List<PurchaseOrderProductItem> purchaseOrderProduct = warehouseBinDMapper.getPurchaseOrderProduct(LocalDateUtils.format(startDate,LocalDateUtils.DATE_PATTERN)+" 00:00:00",LocalDateUtils.format(enddate,LocalDateUtils.DATE_PATTERN)+" 23:59:59");
        //待到货发送消息
        receiveSendingMessages(purchaseOrderProduct);
        //获取日志位按时输入订单的
        List<PurchaseOrderProductItem> purchaseOrderProduct2 = warehouseBinDMapper.getPurchaseOrderProduct2(LocalDateUtils.format(startDate, LocalDateUtils.DATE_PATTERN) + " 00:00:00", LocalDateUtils.format(enddate, LocalDateUtils.DATE_PATTERN) + " 23:59:59");
        receiveSendingMessages2(purchaseOrderProduct2);
    }

    //未更新的记录
    private void receiveSendingMessages3(List<PurchaseOrderProductItem> list) {


    }

    private void receiveSendingMessages2(List<PurchaseOrderProductItem> list) {
        List<PurchaseOrderProductItem> list1 = list.stream().filter(x -> x.getId() == null).collect(Collectors.toList());
        Map<Long, List<PurchaseOrderProductItem>> collect = list1.stream().collect(Collectors.groupingBy(PurchaseOrderProductItem::getOptUid));
        for (Map.Entry<Long, List<PurchaseOrderProductItem>> entry : collect.entrySet()) {
            //获取订单
            List<PurchaseOrderProductItem> purchaseOrderProductItems = collect.get(entry.getKey());
            //获取这个采购员的手机号码
            SysUser sysUser = sysUserService.getById(entry.getKey());
//            UUID uuid = UUID.randomUUID();
//            String replace = uuid.toString().replace("-", "");
           long currentTimeMillis = System.currentTimeMillis();
            if (sysUser.getMobile()!= null) {
                //发送消息"("+currentTimeMillis+")订单:"
                String collect1="("+currentTimeMillis+")订单:";
                 collect1 += purchaseOrderProductItems.stream().map(X -> X.getPurchaseOrderCode()+",").collect(Collectors.joining());
                R<String> stringR = dingDingUtil.userInfo(sysUser.getMobile());
                if (stringR.isSuccess()) {
                    String userId = stringR.getData();
                    JSONObject jsonObject = DingTalkUtil.sendDingMessage(dingdingConfig.APPKEY, dingdingConfig.APPSECRET, dingdingConfig.AGENTID, userId, collect1+":没有提交日志");
//                    System.out.println(jsonObject.toJSONString());
                }
            }
        }
    }
    private void receiveSendingMessages(List<PurchaseOrderProductItem> list1) {
        //按照采购员分组
        Map<Long, List<PurchaseOrderProductItem>> collect = list1.stream().collect(Collectors.groupingBy(PurchaseOrderProductItem::getOptUid));
        //遍历map
        for (Map.Entry<Long, List<PurchaseOrderProductItem>> entry : collect.entrySet()) {
            //获取订单
            List<PurchaseOrderProductItem> purchaseOrderProductItems = collect.get(entry.getKey());
            //获取这个采购员的手机号码
            SysUser sysUser = sysUserService.getById(entry.getKey());
            if (sysUser.getMobile()!= null) {
                //按照订单分组
                Map<String, List<PurchaseOrderProductItem>> collect2 = purchaseOrderProductItems.stream().collect(Collectors.groupingBy(PurchaseOrderProductItem::getPurchaseOrderCode));
//                UUID uuid = UUID.randomUUID();
//                String replace = uuid.toString().replace("-", "");
                long currentTimeMillis = System.currentTimeMillis();
                StringBuilder stringbuilder = new StringBuilder("("+currentTimeMillis+")订单即将延期：");
                for (Map.Entry<String, List<PurchaseOrderProductItem>> entry2 : collect2.entrySet()) {
                    String key = entry2.getKey();
                    stringbuilder.append(key+",");
                }
                //发送消息
                R<String> stringR = dingDingUtil.userInfo(sysUser.getMobile());
                if (stringR.isSuccess()) {
                    String userId = stringR.getData();
                    JSONObject jsonObject = DingTalkUtil.sendDingMessage(dingdingConfig.APPKEY, dingdingConfig.APPSECRET, dingdingConfig.AGENTID, userId, stringbuilder.toString());
                }
            }
        }
    }


}
